import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: !!uni.getStorageSync('userInfo'),
		userInfo: (() => {
			try {
				const userInfo = uni.getStorageSync('userInfo');
				return userInfo ? JSON.parse(userInfo) : {};
			} catch (error) {
				console.warn('解析用户信息失败:', error);
				return {};
			}
		})()
	},
	mutations: {
		login(state, userInfo) {
			state.hasLogin = true
			state.userInfo = userInfo
			uni.setStorageSync('userInfo', JSON.stringify(userInfo))
		},
		logout(state) {
			state.hasLogin = false
			state.userInfo = {}
			//localStorage.removeItem('userInfo')
			uni.removeStorageSync('userInfo');
			uni.removeStorageSync('token');
		}
	},
	actions: {

	}
})

export default store